package com.demo.commons.exception.customize;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *@author zwy
 *@date 2023/7/20 18:20
 * ajax传输方式
*/
public class ModelAndViewR implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView mv = new ModelAndView();
        //是否为ajax
        String header = request.getHeader("X-Requested-With");
        if (!StringUtils.hasText(header)) {
            //判断是否为null或者“”spring提供
            mv.setViewName("custom");
        } else {//是ajax处理
            //导入依赖fastjson
            FastJsonJsonView fastjson = new FastJsonJsonView();
            mv.addObject(fastjson);
        }
        return mv;
    }
}